package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.UserAddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user/addressBook")
@RestController
public class UserAddressBookController {
    @Autowired
    private UserAddressBookService userAddressBookService;

    @PostMapping
    public Result setAddressBook(@RequestBody AddressBook addressBook) {
        userAddressBookService.setAddressBook(addressBook);

        return Result.success();

    }

    @GetMapping("list")
//  @Cacheable(cacheNames = "addressBookeCache", key = "#string")
    public Result<List<AddressBook>> list() {
        Long currentId = BaseContext.getCurrentId();
        String string = currentId.toString();
        List<AddressBook> addressBookslist = userAddressBookService.list(currentId);

        return Result.success(addressBookslist);
    }

 @GetMapping("/default")
 public  Result<AddressBook> setDefult(){

   AddressBook addressBook= userAddressBookService.setDefult();
   return Result.success(addressBook);
    }




    @PutMapping("/default")
    public Result putDefaultAddressBook(@RequestBody AddressBook addressBook) {

        userAddressBookService.updateAddressBook(addressBook);

        return Result.success();
    }
    @PutMapping
    public  Result ipdateAddressBook(@RequestBody AddressBook addressBook){

userAddressBookService.updateAddressBook(addressBook);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<AddressBook> setById(@PathVariable Long id){
        AddressBook addressBook =userAddressBookService.setById(id);
        return Result.success(addressBook);
    }
    @DeleteMapping
    public Result DeleteAddressBook(Long id){
        userAddressBookService.DeleteAddressBook(id);
        return Result.success();
    }
}
